/**
 * 详情(PDF)  物业须知详情,最新通知详情,管理制度详情,物业通知详情统一在这个js。
 * @author truexin
 * @date   2016/07/22
 */

import PullRefresh from '../components/mui/PullRefresh.js';

(function (lf, r) {
    
    let ajax = lf.utils.ajax,
        RC = r.createClass,
        setTitle = lf.bridgeJs.setTitle,
        doAction = lf.bridgeJs.doAction,
        htmlUrl = location.href,
        getUrlParam = lf.utils.getUrlParam;
    
    const PDF = 'https://pdf.h5.liefengtech.com/web/viewer.html?disableHistory=true&file=',
        NOTICE_NEW_INTRO = 'notice-new-intro',
        PROPERTY_INSTRUCTIONS_DETAIL = 'property-instructions-detail';
    
    let data = htmlUrl.indexOf(PROPERTY_INSTRUCTIONS_DETAIL) > -1 ?
    {
        id: getUrlParam('id')
    } :
    {
        noticeId: getUrlParam('noticeId'),
    };
    
    let pdfColor = htmlUrl.indexOf(NOTICE_NEW_INTRO) > -1 ? 'text-green' : 'text-blue';
    
    let Body = RC({
        getInitialState: function () {
            return ({
                text: '',
                title: '',
                isPDF: false,
            })
        },
        componentWillMount: function () {
            
            let api = htmlUrl.indexOf(NOTICE_NEW_INTRO) > -1 ?
                lf.api.notice.getNotice :
                (htmlUrl.indexOf(PROPERTY_INSTRUCTIONS_DETAIL) > -1 ?
                    lf.finger.api.property.getProjectNotice :
                    lf.finger.api.property.getNotice);
            
            let me = this;
            
            ajax({
                data: data,
                success: function (data) {
                    
                    if (data.code == '200') {
                        
                        let isPDF = data.data.content.indexOf('.pdf') > -1 ? true : false;
                        
                        me.setState({
                            text: data.data.content,
                            title: data.data.title,
                            isPDF: isPDF
                        });
                        
                        setTitle(data.data.title);
                    }
                    else {
                        alert(data.desc);
                    }
                }
            }, api);
        },
        componentDidUpdate: function () {
            
            let node = r.findDOMNode(this.refs.innerHtml);
            node.innerHTML = this.state.text;
            
            let $links = $(node).find('a');
            $.each($links, function () {
                let $me = $(this),
                    $href = $me.attr('href'),
                    $title = $me.attr('title'),
                    url = PDF + encodeURIComponent($href);
                $me.parent().find('img').addClass('ele-hide');
                $me.removeAttr('href');  //阻止a默认跳转
                
                if ($href && $href.indexOf('.pdf') > -1) {
                    $me.addClass('link icon-pdf text-dot ' + pdfColor);
                    $me.off();
                    $me.on('tap', function () {
                        doAction(url, $title)
                    })
                }
            })
        },
        render: function () {
            return (
                <div id="noticeDetail" className="text-with-pdf">
                    <h1 className="title">{this.state.title}</h1>
                    <div ref="innerHtml" className="rich-text"></div>
                </div>
            );
        }
    });
    
    lf.appJs.getUser(function (user) {
        
        let noticeId = getUrlParam('noticeId'),
            api = htmlUrl.indexOf(NOTICE_NEW_INTRO) > -1 ?
                lf.api.notice.saveNoticeBrowse :
                lf.finger.api.property.saveNoticeBrowse;
        
        if (noticeId) { //个推浏览记录更新才有,此时需要调用接口
            ajax({
                type: "POST",
                data: {
                    staffId: user.id, //oa
                    
                    noticeId: noticeId, //公共参数
                    
                    userType: user.householdType,//property
                    userId: user.id,
                    
                },
                success: function (data) {
                    if (data.code == '200') {
                        console.log('you are browsing!');
                    }
                }
            }, api);
        }
        
        r.render(
            <PullRefresh data={<Body />}/>,
            document.getElementById('root'), function () {
            }
        )
    })
    
})(liefeng, React);